package com.lxw.station.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
@Slf4j
public class Chrome {
    @SneakyThrows
    public static void main(String[] args) {
        String json = new String(Files.readAllBytes(Paths.get("chrome")), StandardCharsets.UTF_8);
        JSONArray versions = JSON.parseObject(json).getJSONArray("versions");
        String path = "D:\\chrome\\";
        for (int i = 0; i < versions.size(); i++) {
            JSONObject version = versions.getJSONObject(i);
            String versionString = version.getString("version");
            JSONObject downloads = version.getJSONObject("downloads");
            JSONArray chrome = downloads.getJSONArray("chrome");
            JSONArray chromedriver = downloads.getJSONArray("chromedriver");
            System.out.println(versionString);
            try {
                if (chromedriver != null) {
                    String chromeFile = path+versionString+"\\chrome\\";
                    for (int j = 0; j < chrome.size(); j++) {
                        String url = chrome.getJSONObject(j).getString("url");
                        String fileName = url.substring(url.lastIndexOf("/") + 1);
                        downloadZipFile(url,chromeFile+fileName);
                    }
                    String chromedriverFile = path+versionString+"\\chromedriver\\";
                    for (int j = 0; j < chromedriver.size(); j++) {
                        String url = chromedriver.getJSONObject(j).getString("url");
                        String fileName = url.substring(url.lastIndexOf("/") + 1);
                        downloadZipFile(url,chromedriverFile+fileName);
                    }
                }
            }catch (Exception e){
                log.error("下载失败:{}",versionString);
            }
        }
    }

    public static void downloadZipFile(String url, String destFilePath) throws IOException {
        File file = new File(destFilePath).getParentFile();
        if (!file.exists()) {
            file.mkdirs();
        }
        RestTemplate restTemplate = new RestTemplate();

        // 发送 GET 请求获取文件
        ResponseEntity<Resource> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null,
                Resource.class
        );

        // 获取输入流
        Resource resource = response.getBody();
        if (resource == null) {
            throw new IOException("Failed to retrieve resource");
        }

        try (InputStream inputStream = resource.getInputStream();
             OutputStream outputStream = new FileOutputStream(new File(destFilePath))) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}
